<?php

require_once(dirname(__FILE__)."/dao.php");

class LocationDao extends Dao
{
    public function __construct()
    {
        parent::__construct();
        $this->_table = "tbl_location";
    }

    public function getMacAddress($username, $location)
    {
        $sql = sprintf("SELECT * FROM %s WHERE username = '%s' AND location = '%s'", $this->_table, $username, $location); 
        $ret = $this->_query($sql);
        if (!$ret)
        {
            return null;
        }
        return $ret[0];
    }

    public function getLocation($username, $macAddress)
    {
        $sql = sprintf("SELECT * FROM %s WHERE username = '%s' AND mac_address = '%s'", $this->_table, $username, $macAddress); 
        $ret = $this->_query($sql);
        if (!$ret)
        {
            return null;
        }
        return $ret[0];
    }

    public function setLocation($username, $macAddress,$location)
    {
        $sql = sprintf("SELECT * FROM %s WHERE username = '%s' AND mac_address = '%s' AND location = '%s'", $this->_table, $username, $macAddress, $location); 
        $ret = $this->_query($sql);
        
        if (!$ret)
        {
            $sql = sprintf("INSERT INTO %s (username,mac_address, location) VALUES ('%s', '%s', '%s')", $this->_table, $username, $macAddress, $location); 
        }
        else
        {
            $sql = sprintf("UPDATE %s SET mac_address = '%s' WHERE username = '%s' AND location = '%s'", $this->_table, $macAddress, $username, $location); 
        }
        $ret = $this->_query($sql);
        //return $ret;
        return 0;
    }
};
/*$a = new LocationDao();
$a->setLocation("liming", "aa-bb-cc-dd-11-22", 2);
$ret = $a->getLocation("liming", "aa-bb-cc-dd-11-22");
var_export($ret);

 */
